package thu.cs.segroup.agent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import thu.cs.segroup.agent.behaviour.RunnerConfigTestBehav;
import thu.cs.segroup.agent.behaviour.RunnerInterpreter;
import thu.cs.segroup.entity.HostState;
import thu.cs.segroup.entity.TestTask;
import thu.cs.segroup.rule.RuleEngine;
import thu.cs.segroup.test.SendMsgBehavTest;
import jade.core.Agent;

public class Runner extends Agent {

	private TestTask task;
	private List<HostState> hosts;
	private HashMap<String, Object> execConfig;
	private String coor;
	private RuleEngine re;
	private HashMap<String, Object> props;
	
	@Override
	protected void setup() {
		System.out.println(this.getLocalName() + " started");
		Object[] args = this.getArguments();
		re = (RuleEngine)args[0];
		super.setup();
		task = null;
		hosts = new ArrayList<HostState>();
		execConfig = new HashMap<String, Object>();
		props = new HashMap<String, Object>();
		props.put("name", this.getLocalName());
		this.addBehaviour(new RunnerInterpreter(this));
		
		
		
		// this.addBehaviour(new SendMsgBehavTest("TEST", "Coordinator", this));
	}
	
	public TestTask getTask() {
		return task;
	}
	
	public void setTask(TestTask _task) {
		this.task = _task;
	}
	
	public void saveOrUpdateHost(HostState _host_state) {
		boolean find = false;
		for (int i = 0; i < hosts.size(); i++) {
			if (hosts.get(i).getId().equals(_host_state.getId())) {
				find = true;
				hosts.get(i).setState(_host_state.getState());
			}
		}
		if (!find) {
			hosts.add(_host_state);
		}
	}
		
	public String getCoor() {
		return coor;
	}

	public void setCoor(String coor) {
		this.coor = coor;
	}

	@Override
	protected void takeDown() {
	}

	@Override
	protected void afterMove() {
		this.addBehaviour(new RunnerConfigTestBehav(this));
	}
	
	public RuleEngine getRuleEngine() {
		return re;
	}
	
	public void addProp(String key, Object value) {
		props.put(key, value);
	}
	
	public Object getProp(String key) {
		return props.get(key);
	}
	
}
